package org.tabletopmachine.board;


import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.*;
import org.tabletopmachine.IDrawable;


/**
 * Class to display a ruler while pressing down the left mouse button
 * 
 * @author Peter Eichinger
 * 
 */
public class Ruler implements IDrawable {
        /** Indicates if the ruler should be drawn or not */
        private boolean _drawRuler;
        /** Start position of the ruler */
        private Vector2f _rulerStart;
        /** End position of the ruler */
        private Vector2f _rulerEnd;
        /** Color of the ruler */
        private Color _color;
        private GameBoard _board;


        public Ruler(Color color) {
                _drawRuler = false;
                _rulerStart = new Vector2f(0, 0);
                _rulerEnd = new Vector2f(0, 0);
                _color = color;
                _board = GameBoard.getBoard();
        }


        /**
         * update if ruler should be drawn, and the location
         */
        @Override
        public void update(GameContainer gc, int delta) {
                Input input = gc.getInput();


                if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
                        _drawRuler = true;
                        _rulerStart = new Vector2f(input.getMouseX(), input.getMouseY());
                }
                if (_drawRuler && (!input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))) {
                        _drawRuler = false;
                }
                _rulerEnd = new Vector2f(input.getMouseX(), input.getMouseY());
        }


        /**
         * draw the ruler
         */
        @Override
        public void render(GameContainer gc, Graphics g) {
                if (_drawRuler) {
                        Color PrevColor = g.getColor();
                        float LineWidth = g.getLineWidth();
                        g.setColor(_color);
                        g.setLineWidth(2.0f);
                        Line ruler = new Line(_rulerStart, _rulerEnd);
                        float[] norm = ruler.getNormal(0);


                        Line startcross = new Line(_rulerStart.x - 5 * norm[0], _rulerStart.y - 5 * norm[1], _rulerStart.x + 5 * norm[0], _rulerStart.y + 5 * norm[1]);


                        Vector2f NormalizedRuler = new Vector2f(_rulerEnd.x - _rulerStart.x, _rulerEnd.y - _rulerStart.y);
                        Vector2f NormalizedRulerInches = _board.ConvertPixelsToInches(NormalizedRuler);
                        float rulerlength = NormalizedRulerInches.length();


                        Line endcross = new Line(_rulerEnd.x - 5 * norm[0], _rulerEnd.y - 5 * norm[1], _rulerEnd.x + 5 * norm[0], _rulerEnd.y + 5 * norm[1]);
                        g.draw(ruler);
                        g.draw(startcross);
                        g.draw(endcross);


                        g.setColor(PrevColor);
                        g.setLineWidth(LineWidth);


                        g.drawString(String.format("%.1f", rulerlength), _rulerEnd.x, _rulerEnd.y - 20);


                }
        }
}